import { http } from '@/utils/request'

const prefix = '/schedule'

// 查询定时任务调度列表
export function getListJob(query?: any) {
	return http.get<ResPage<any>>(`${prefix}/job/list`, query)
}

// 任务状态修改
export function changeJobStatus(jobId: string, status: string) {
	const data = {
		jobId,
		status
	}
	return http.put(`${prefix}/job/changeStatus`, data)
}

// 导出
export function jobExport(data: any) {
	return http.postFile<ExportResponse>(`${prefix}/job/export`, data, {
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		responseType: 'blob'
	})
}

// 定时任务立即执行一次
export function runJob(jobId: string, jobGroup: string) {
	const data = {
		jobId,
		jobGroup
	}
	return http.put(`${prefix}/job/run`, data)
}

// 删除定时任务调度
export function delJob(jobId: string) {
	return http.delete(`${prefix}/job/${jobId}`)
}

// 查询定时任务调度详细
export function getJob(jobId: string) {
	return http.get<any>(`${prefix}/job/${jobId}`)
}

// 新增定时任务调度
export function addJob(data: any) {
	return http.post(`${prefix}/job`, data)
}

// 修改定时任务调度
export function updateJob(data: any) {
	return http.put(`${prefix}/job`, data)
}

// 查询调度日志列表
export function listJobLog(params: any) {
	let apiParams = { ...params }
	if (params.daterange) {
		apiParams['params[beginTime]'] = params.daterange[0]
		apiParams['params[endTime]'] = params.daterange[1]

		delete apiParams.daterange
	}
	return http.get<ResPage<any>>(`${prefix}/job/log/list`, apiParams)
}

// 删除调度日志
export function delJobLog(jobLogId: string) {
	return http.delete(`${prefix}/job/log/${jobLogId}`)
}

// 清空调度日志
export function cleanJobLog() {
	return http.delete(`${prefix}/job/log/clean`)
}

// 调度日志导出
export function jobLogExport(data: any) {
	return http.postFile<ExportResponse>(`${prefix}/job/log/export`, data, {
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		responseType: 'blob'
	})
}

// 查询定时任务调度详细
export function getJobById(jobId: string) {
	return http.get<any>(`${prefix}/job/${jobId}`)
}
